/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import static control.Start.ih;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.AmountOfIngredient;
import model.Ingredient;
import model.Recipe;
import view.RecipePanel;

/**
 *
 * @author Jonas
 */
public class RecipeHandler {

    private ArrayList<Recipe> recipies;
    private ArrayList<AmountOfIngredient> aoiList;
    private DBLogin db;
    private ResultSet rs;

    public RecipeHandler(DBLogin db) {
	this.db = db;
    }

    public ArrayList<RecipePanel> findRecipe(String searchText) {
	ArrayList<RecipePanel> rpFound = new ArrayList();
	RecipePanel rp = null;
	boolean ingFound;
	for (Recipe recipe : recipies) {
	    ingFound = false;
	    if (recipe.getName().toLowerCase().contains(searchText.toLowerCase())) {
		rp = new RecipePanel(recipe);
		rpFound.add(rp);
	    } else {
		for (AmountOfIngredient aoi : recipe.getIngredients()) {
		    if (aoi.getIng().getName().toLowerCase().contains(searchText.toLowerCase())) {
			if (!ingFound) {
			    rp = new RecipePanel(recipe);
			    rpFound.add(rp);
			    ingFound = true;
			}
		    }
		}
	    }
	}
	return rpFound;
    }

    public void createRecipeList() {
	String sql = "SELECT amount.re_id,amount.in_id,amount.amount,recipe.id AS rec_id,recipe.re_name,recipe.image,recipe.description,recipe.cooking_process,recipe.processtime,ingredient.id AS ing_id,ingredient.in_name,ingredient.in_type,ingredient.expire_date,ingredient.unit,ingredient.stock FROM amount join recipe on amount.re_id = recipe.id right outer join ingredient on amount.in_id = ingredient.id ORDER by amount.re_id";
	try {
	    Ingredient ing;
	    Recipe rp;
	    AmountOfIngredient aoi;

	    recipies = new ArrayList<>();

	    rs = db.getStmt().executeQuery(sql);

	    int currentID = 0;

	    while (rs.next()) {
		if (currentID != rs.getInt("re_id")) {
		    aoiList = new ArrayList<>();

		    rp = new Recipe(rs.getString("re_name"), rs.getString("image"), rs.getString("description"), rs.getString("cooking_process"), rs.getInt("processtime"), aoiList);
		    recipies.add(rp);
		}
		ing = ih.getIngredient(Integer.parseInt(rs.getString("ing_id")));
		aoi = new AmountOfIngredient(rs.getInt("amount"), ing);
		aoiList.add(aoi);

		currentID = rs.getInt("re_id");
	    }
	} catch (SQLException ex) {
	    System.out.println("SQL fejl " + ex);
	}
    }

    public void addRecipe(Recipe rp) {
	recipies.add(rp);
	addRecipeToDB(rp);
    }

    public void showAll() {
	for (Recipe recipe : recipies) {
	    System.out.println(recipe.getName());
	    for (AmountOfIngredient aoi : recipe.getIngredients()) {
		System.out.println("\t" + aoi.getIng().getName());
	    }
	}
    }

    public ArrayList<Recipe> getRecipies() {
	return recipies;
    }

    private void addRecipeToDB(Recipe rp) {
	try {
	    String s="";
	    String sql = "SHOW TABLE STATUS LIKE 'recipe'";
	    rs = db.getStmt().executeQuery(sql);
	    int nextAI;
	    rs.next();
	    nextAI=rs.getInt("Auto_increment");
	    
	    sql = "INSERT INTO recipe (re_name,image,description,cooking_process,processtime) VALUES ('" + rp.getName() + "','" + rp.getImage() + "','" + rp.getDescreption() + "','" + rp.getHowtoMethod() + "','" + rp.getCookingTime() + "')";
	    int eu = db.getStmt().executeUpdate(sql);
	    int i=0;
	    for (AmountOfIngredient a : rp.getIngredients()) {
		if(i>0){
		    s+=",";    
		}
		s+="('"+nextAI+"','"+a.getIng().getId()+"','"+a.getAmount()+"')";
		i++;
	    }
	    sql = "INSERT INTO amount (re_id,in_id,amount) VALUES "+s;
	    eu = db.getStmt().executeUpdate(sql);
	} catch (SQLException ex) {
	    System.out.println("SQL FEJL I 'addRecipeToDB': " + ex);
	}
    }
}
